package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.OrderItemQuery;
import cn.org.eshow.bean.query.ProductCommentQuery;
import cn.org.eshow.model.OrderItem;
import cn.org.eshow.model.ProductComment;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.OrderItemManager;
import cn.org.eshow.service.OrderStepManager;
import cn.org.eshow.service.ProductCommentManager;
import cn.org.eshow.service.ProductOrderManager;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址API接口
 */
@AllowedMethods({"comment"})
public class ProductCommentAction extends ApiBaseAction<ProductComment> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductOrderManager productOrderManager;
    @Autowired
    private OrderItemManager orderItemManager;
    @Autowired
    private ProductCommentManager productCommentManager;
    @Autowired
    private OrderStepManager orderStepManager;

    private ProductComment productComment = new ProductComment();
    private List<ProductComment> productComments = new ArrayList<ProductComment>();
    private ProductCommentQuery query = new ProductCommentQuery(Boolean.TRUE);

    /**
     * 订单评价
     */
    public void comment() {
        if (id == null) {
            RenderUtil.failure("缺少参数");
            return;
        }
        ProductOrder old = productOrderManager.get(id);
        if (old == null) {
            RenderUtil.failure("非法参数");
            return;
        }
        if (old.getState() != 2 || old.getCommentState() != 0) {//2完成状态
            RenderUtil.failure("当前订单不处于待评价状态");
            return;
        }

        if(StringUtils.isEmpty(productComment.getContent())){
            RenderUtil.failure("请输入有效内容");
            return;
        }

        List<OrderItem> orderItems=orderItemManager.list(new OrderItemQuery(old.getId()));
        for(OrderItem item:orderItems){
            ProductComment comment=new ProductComment();
            comment.setComment(1);//默认好评
            comment.setUser(old.getUser());
            comment.setEnabled(true);
            comment.setProductOrder(old);
            comment.setProductItem(item.getProductItem());
            comment.setImgs(productComment.getImgs());
            comment.setContent(productComment.getContent());
            comment.setProduct(item.getProduct());
            comment=productCommentManager.save(comment);
        }
        old.setCommentState(1);//1已评价
        old.setStatements("已评价");
        old = productOrderManager.save(old);
        orderStepManager.save("用户已评价", "用户已评价", old);
        RenderUtil.success("提交成功");
        return;
    }

    public ProductComment getProductComment() {
        return productComment;
    }

    public void setProductComment(ProductComment productComment) {
        this.productComment = productComment;
    }

    public List<ProductComment> getProductComments() {
        return productComments;
    }

    public void setProductComments(List<ProductComment> productComments) {
        this.productComments = productComments;
    }

    public ProductCommentQuery getQuery() {
        return query;
    }

    public void setQuery(ProductCommentQuery query) {
        this.query = query;
    }

}